﻿#include "UModePanel.h"
#include <QMessageBox>
#include "CHeader.h"
UModePanel::UModePanel(QWidget *parent)
	: UBasePanel(parent)
{
	ui.setupUi(this);
	m_bStop = false;
	//配置定时器
	m_pTimer = new QTimer(this);
	m_pTimer->setTimerType(Qt::TimerType::PreciseTimer);
	connect(m_pTimer, &QTimer::timeout, this, &UModePanel::slotDataRefresh);
	setStyleSheetFile();
	initConnect();
	m_nRobRunMode = 1;
}

UModePanel::~UModePanel()
{
	if (m_pTimer != NULL)
	{
		if (m_pTimer->isActive())
		{
			m_pTimer->stop();
		}
		delete m_pTimer;
		m_pTimer = NULL;
	}
}

void UModePanel::setStyleSheetFile()
{
}

void UModePanel::initConnect()
{
	connect(ui.pushButtonControlMode, &QPushButton::clicked, this, &UModePanel::slotPushButtonControlModeClicked);
	connect(ui.pushButtonOneOnBack, &QPushButton::clicked, this, &UModePanel::slotPushButtonOneOnBackClicked);
	connect(ui.pushButtonOneOnPower, &QPushButton::clicked, this, &UModePanel::slotPushButtonOneOnPowerClicked);
	connect(ui.pushButtonStopPower, &QPushButton::clicked, this, &UModePanel::slotPushButtonStopPowerClicked);
	connect(ui.pushButtonPauseTask, &QPushButton::clicked, this, &UModePanel::slotPushButtonPauseTaskClicked);
	connect(ui.pushButtonRestoreTask, &QPushButton::clicked, this, &UModePanel::slotPushButtonRestoreTaskClicked);
	connect(ui.pushButtonTaskMode, &QPushButton::clicked, this, &UModePanel::slotPushButtonTaskModeClicked);
	connect(ui.pushButtonTerminatTask, &QPushButton::clicked, this, &UModePanel::slotPushButtonTerminatTaskClicked);
	connect(ui.pushButtonMore, &QPushButton::clicked, this, &UModePanel::slotPushButtonMore);
	ui.pushButtonMore->hide();
}

void UModePanel::refreshData(bool bRefresh)
{
	if (bRefresh)
	{
		m_pTimer->start(1000);
	}
	else
	{
		m_pTimer->stop();
	}
}

void UModePanel::slotDataRefresh()
{
	if (isVisible())
	{
		m_nRobRunMode = m_pDevData->m_nRobRunMode;
		//根据当前的工作模式禁用按钮
		if (m_nRobRunMode == 0)//遥控模式
		{
			ui.pushButtonControlMode->setIsTagVisible(true);
			ui.pushButtonTaskMode->setIsTagVisible(false);
			ui.pushButtonControlMode->setEnabled(true);
			ui.pushButtonOneOnPower->setEnabled(true);
			ui.pushButtonStopPower->setEnabled(true);
			ui.pushButtonOneOnBack->setEnabled(true);
			ui.pushButtonTaskMode->setEnabled(true);
			ui.pushButtonPauseTask->setEnabled(false);
			ui.pushButtonRestoreTask->setEnabled(false);
			ui.pushButtonTerminatTask->setEnabled(false);
			ui.pushButtonMore->setEnabled(true);
		}
		else if (m_nRobRunMode == 1)//任务模式
		{
			ui.pushButtonControlMode->setIsTagVisible(false);
			ui.pushButtonTaskMode->setIsTagVisible(true);
			ui.pushButtonControlMode->setEnabled(true);
			ui.pushButtonOneOnPower->setEnabled(false);
			ui.pushButtonStopPower->setEnabled(false);
			ui.pushButtonOneOnBack->setEnabled(false);
			ui.pushButtonTaskMode->setEnabled(true);
			ui.pushButtonPauseTask->setEnabled(true);
			ui.pushButtonRestoreTask->setEnabled(true);
			ui.pushButtonTerminatTask->setEnabled(true);
			ui.pushButtonMore->setEnabled(false);
		}
	}
}

void UModePanel::slotPushButtonControlModeClicked()
{
	//首先要判断是否已经在遥控模式
	if (m_nRobRunMode == 0)
	{
		return;
	}
	else//当前处于任务模式下判断一下是否有任务在执行
	{
		if (m_pDevData->m_nTaskStatus == 1 || m_pDevData->m_nTaskStatus == 2)//
		{
			int nRet = messageBox(3, A2T("当前存在任务在执行,是否将当前任务中止后再切换到遥控模式"));
			if (nRet == QMessageBox::Yes)
			{
				slotPushButtonTerminatTaskClicked();
			}
			else
			{
				return;
			}
		}
	}

	ui.pushButtonControlMode->setEnabled(true);//遥控模式
	ui.pushButtonOneOnBack->setEnabled(true);//一键返航
	ui.pushButtonOneOnPower->setEnabled(true);//一键充电
	ui.pushButtonStopPower->setEnabled(true);//停止充电
	ui.pushButtonTaskMode->setEnabled(true);//任务模式
	ui.pushButtonPauseTask->setEnabled(false);//暂停任务
	ui.pushButtonRestoreTask->setEnabled(false);//恢复任务
	ui.pushButtonTerminatTask->setEnabled(false);//中止任务

	SDevExecLog sLog;
	sLog.m_dLogTime = QDateTime::currentDateTime();
	sLog.m_nAlarmLevel = enWorkInfo;
	sLog.m_sDescriptionl = A2T("%1点击了%2的%3按钮")
		.arg("admin")
		.arg(m_pDevData->m_sRobName)
		.arg(A2T("遥控模式"));
	sLog.m_sDevName = A2T("机器人远程操控");
	m_pDevData->addDevExecLog(sLog);
	SCtrInfo stCtrInfo;
	stCtrInfo.m_sGuid = m_pDevData->m_sDevGuid;
	stCtrInfo.m_nCtrCmd = ECCmdType::enCWitchModeManual;
	stCtrInfo.m_bHasCtr = false;
	stCtrInfo.m_bCtrSync = true;
	m_pRobCtrMange->ctrDevice(stCtrInfo);
}

void UModePanel::slotPushButtonOneOnPowerClicked()
{
	SDevExecLog sLog;
	sLog.m_dLogTime = QDateTime::currentDateTime();
	sLog.m_nAlarmLevel = enWorkInfo;
	sLog.m_sDescriptionl = A2T("%1点击了%2的%3按钮")
		.arg("admin")
		.arg(m_pDevData->m_sRobName)
		.arg(A2T("一键充电"));
	sLog.m_sDevName = A2T("机器人远程操控");
	m_pDevData->addDevExecLog(sLog);
	SCtrInfo stCtrInfo;
	stCtrInfo.m_sGuid = m_pDevData->m_sDevGuid;
	stCtrInfo.m_nCtrCmd = ECCmdType::enCStartCharging;
	stCtrInfo.m_bHasCtr = false;
	stCtrInfo.m_bCtrSync = true;
	m_pRobCtrMange->ctrDevice(stCtrInfo);
}

void UModePanel::slotPushButtonStopPowerClicked()
{
	SDevExecLog sLog;
	sLog.m_dLogTime = QDateTime::currentDateTime();
	sLog.m_nAlarmLevel = enWorkInfo;
	sLog.m_sDescriptionl = A2T("%1点击了%2的%3按钮")
		.arg("admin")
		.arg(m_pDevData->m_sRobName)
		.arg(A2T("停止充电"));
	sLog.m_sDevName = A2T("机器人远程操控");
	m_pDevData->addDevExecLog(sLog);
	SCtrInfo stCtrInfo;
	stCtrInfo.m_sGuid = m_pDevData->m_sDevGuid;
	stCtrInfo.m_nCtrCmd = ECCmdType::enCStopCharge;
	stCtrInfo.m_bHasCtr = false;
	m_pRobCtrMange->ctrDevice(stCtrInfo);
}

void UModePanel::slotPushButtonOneOnBackClicked()
{
	SDevExecLog sLog;
	sLog.m_dLogTime = QDateTime::currentDateTime();
	sLog.m_nAlarmLevel = enWorkInfo;
	sLog.m_sDescriptionl = A2T("%1点击了%2的%3按钮")
		.arg("admin")
		.arg(m_pDevData->m_sRobName)
		.arg(A2T("一键返航"));
	sLog.m_sDevName = A2T("机器人远程操控");
	m_pDevData->addDevExecLog(sLog);
	SCtrInfo stCtrInfo;
	stCtrInfo.m_sGuid = m_pDevData->m_sDevGuid;
	stCtrInfo.m_nLocaVal = 0;
	stCtrInfo.m_nCtrCmd = ECCmdType::enCMoveToPosition;
	stCtrInfo.m_bHasCtr = false;
	m_pRobCtrMange->ctrDevice(stCtrInfo);
}

void UModePanel::slotPushButtonTaskModeClicked()
{
	ui.pushButtonControlMode->setEnabled(true);//遥控模式
	ui.pushButtonOneOnBack->setEnabled(false);//一键返航
	ui.pushButtonOneOnPower->setEnabled(false);//一键充电
	ui.pushButtonStopPower->setEnabled(false);//停止充电
	ui.pushButtonTaskMode->setEnabled(true);//任务模式
	ui.pushButtonPauseTask->setEnabled(true);//暂停任务
	ui.pushButtonRestoreTask->setEnabled(true);//恢复任务
	ui.pushButtonTerminatTask->setEnabled(true);//中止任务

	SDevExecLog sLog;
	sLog.m_dLogTime = QDateTime::currentDateTime();
	sLog.m_nAlarmLevel = enWorkInfo;
	sLog.m_sDescriptionl = A2T("%1点击了%2的%3按钮")
		.arg("admin")
		.arg(m_pDevData->m_sRobName)
		.arg(A2T("任务模式"));
	sLog.m_sDevName = A2T("机器人远程操控");
	m_pDevData->addDevExecLog(sLog);
	SCtrInfo stCtrInfo;
	stCtrInfo.m_sGuid = m_pDevData->m_sDevGuid;
	stCtrInfo.m_nCtrCmd = ECCmdType::enCWitchModeAuto;
	stCtrInfo.m_bHasCtr = false;
	m_pRobCtrMange->ctrDevice(stCtrInfo);
}

void UModePanel::slotPushButtonPauseTaskClicked()
{
	SDevExecLog sLog;
	sLog.m_dLogTime = QDateTime::currentDateTime();
	sLog.m_nAlarmLevel = enWorkInfo;
	sLog.m_sDescriptionl = A2T("%1点击了%2的%3按钮")
		.arg("admin")
		.arg(m_pDevData->m_sRobName)
		.arg(A2T("暂停任务"));
	sLog.m_sDevName = A2T("机器人远程操控");
	m_pDevData->addDevExecLog(sLog);
	SCtrInfo stCtrInfo;
	stCtrInfo.m_sGuid = m_pDevData->m_sDevGuid;
	stCtrInfo.m_nCtrCmd = ECCmdType::enCSuspendTask;
	stCtrInfo.m_bHasCtr = false;
	bool bRet = m_pRobCtrMange->ctrDevice(stCtrInfo);
}

void UModePanel::slotPushButtonRestoreTaskClicked()
{
	SDevExecLog sLog;
	sLog.m_dLogTime = QDateTime::currentDateTime();
	sLog.m_nAlarmLevel = enWorkInfo;
	sLog.m_sDescriptionl = A2T("%1点击了%2的%3按钮")
		.arg("admin")
		.arg(m_pDevData->m_sRobName)
		.arg(A2T("恢复任务"));
	sLog.m_sDevName = A2T("机器人远程操控");
	m_pDevData->addDevExecLog(sLog);
	SCtrInfo stCtrInfo;
	stCtrInfo.m_sGuid = m_pDevData->m_sDevGuid;
	stCtrInfo.m_nCtrCmd = ECCmdType::enCRestartTask;
	stCtrInfo.m_bHasCtr = false;
	bool bRet = m_pRobCtrMange->ctrDevice(stCtrInfo);
}

void UModePanel::slotPushButtonTerminatTaskClicked()
{
	SDevExecLog sLog;
	sLog.m_dLogTime = QDateTime::currentDateTime();
	sLog.m_nAlarmLevel = enWorkInfo;
	sLog.m_sDescriptionl = A2T("%1点击了%2的%3按钮")
		.arg("admin")
		.arg(m_pDevData->m_sRobName)
		.arg(A2T("中止任务"));
	sLog.m_sDevName = A2T("机器人远程操控");
	m_pDevData->addDevExecLog(sLog);
	SCtrInfo stCtrInfo;
	stCtrInfo.m_sGuid = m_pDevData->m_sDevGuid;
	stCtrInfo.m_nCtrCmd = ECCmdType::enCAbortTask;
	stCtrInfo.m_bHasCtr = false;
	bool bRet = m_pRobCtrMange->ctrDevice(stCtrInfo);
	m_pDevData->m_bTaskExec = false;
}

void UModePanel::slotPushButtonMore()
{
	/*CRobMoreDlg oper(m_pRobCtrMange, m_pDevData);
	oper.exec();*/
}
